﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using projet_fichier_objet;
using FichierTxt;
using Outils;
using System.Collections.Generic;
using System.Data.SqlClient;
using App_Recherche;

namespace App_Recherche
{
    public partial class resultat : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //récupération des paramètre de l'url
            string Nomcherche = Request.QueryString["nom"];
            string Idrechercher = Request.QueryString["id"];
            string TypeRecherche = Request.QueryString["Recherche"];
            string CodeAgence = Request.QueryString["Ag"];

            //boolèen si aucun résultat
            bool hasResults = false;
            //affecte la methode rptPersonnes_ItemDataBound lorsque l'on va boucler sur les données

            List<Personne> ListInterimaire = new List<Personne>();
            //string CnxInterimaire = "Data Source=PILOTT-LGZ;Initial Catalog=Interimaires;Integrated Security=True;Pooling=False";
            SqlConnection SqlC = new SqlConnection(Utils.CnxInterimaire);
            SqlC.Open();
            SqlCommand SQLCmd = SqlC.CreateCommand();
            int Recherche = 0;
            int CodeAg = 0;
            if (int.TryParse(CodeAgence, out CodeAg))
            {
                if (int.TryParse(TypeRecherche, out Recherche))
                {
                    if (CodeAg != 0)
                    {
                        if (Recherche == 1)
                        {
                            SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Agence = '" + CodeAgence + "' AND Nom LIKE '" + Nomcherche + "%'";
                        }
                        else if (Recherche == 2)
                        {
                            SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Agence = '" + CodeAgence + "' AND Nom LIKE '%" + Nomcherche + "'";
                        }
                        else if (Recherche == 3)
                        {
                            SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Agence = '" + CodeAgence + "' AND Nom LIKE '%" + Nomcherche + "%'";
                        }
                        else
                        {
                            SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Agence = '" + CodeAgence + "' AND Nom = '" + Nomcherche + "'";
                        }
                    }
                    else
                    {
                        if (Recherche == 1)
                        {
                            SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Nom LIKE '" + Nomcherche + "%'";
                        }
                        else if (Recherche == 2)
                        {
                            SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Nom LIKE '%" + Nomcherche + "'";
                        }
                        else if (Recherche == 3)
                        {
                            SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Nom LIKE '%" + Nomcherche + "%'";
                        }
                        else
                        {
                            SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Nom = '" + Nomcherche + "'";
                        }
                    }
                }
            }

            SqlDataReader SqlReader = SQLCmd.ExecuteReader();
            while (SqlReader.Read() && SqlReader != null)
            {
                Personne candidat = new Personne();

                int ID = 0;
                if (int.TryParse(SqlReader["Id"].ToString(), out ID))
                {
                    candidat.ID = ID;
                }
                candidat.Nom = SqlReader["Nom"].ToString();
                candidat.Email = SqlReader["Email"].ToString();
                candidat.Ville = SqlReader["Ville"].ToString();
                candidat.DateNaissance = Convert.ToDateTime(SqlReader["DateNaiss"]);
                candidat.CodeAgence = (int)SqlReader["Agence"];
                hasResults = true;

                ListInterimaire.Add(candidat);
            }
            SqlC.Close();

            if (hasResults)
            {
                imgAuncunResultat.Visible = false;
                rptPersonnes.Personnes = ListInterimaire;
            }
            else
            {
                imgAuncunResultat.Visible = true;
            }
        }
    }
}
